/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.wechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.bifrost.wechat.service.WxServiceContext;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 *获取accessToken
 *
 *@author joewee
 *@version 1.0.0
 *@Date 2018/4/22 16:52
 */
@RestController
@RequestMapping("/wechat/accessToken")
public class AccessTokenController {
    @Value("${cbest.accessKey:test_rrl}")
    private  String accessKey;
    @Autowired
    WxServiceContext context;
    @PostMapping
    public Map getToken(@RequestBody JSONObject json, @RequestHeader("Signature") String signature){
        Map returnMsg = new HashMap(4);
        if(!json.containsKey("appid")){
            returnMsg.put("errCode",-1);
            returnMsg.put("errMessage","参数appid不能为空");
            return  returnMsg;
        }
        String appid = json.getString("appid");
        if(StringUtils.isNotBlank(signature)){
            if(!signature.toUpperCase().equals(DigestUtils.md5Hex(accessKey.getBytes()).toUpperCase())){
                returnMsg.put("errCode",-1);
                returnMsg.put("errMessage","签名错误");
                return  returnMsg;
            }else{
                if (StringUtils.isBlank(appid)) {
                    returnMsg.put("errCode",-1);
                    returnMsg.put("errMessage","参数appid不能为空");
                    return  returnMsg;
                }
               WxMpService service = context.getWxServiceByAppId(appid) ;
                try {
                    Map data = new HashMap(2);
                    boolean forceRefresh =false;
                    if (json.containsKey("forceRefresh")) {
                        forceRefresh = json.getBoolean("forceRefresh");
                    }
                    returnMsg.put("errCode",0);
                    returnMsg.put("data",data);
                    data.put("access_token",service.getAccessToken(forceRefresh));
                    data.put("expiresin",service.getWxMpConfigStorage().getExpiresTime());
                    return  returnMsg;
                } catch (WxErrorException e) {
                    e.printStackTrace();
                }
            }
        }
        returnMsg.put("errCode",-1);
        returnMsg.put("errMessage","签名错误");
        return  returnMsg;
    }
}
